package com.hualan.controller;

import com.hualan.pojo.Company;
import com.hualan.service.CompanyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Autowired
    private CompanyService companyService;

    // 获取所有公司信息
    @GetMapping
    public ResponseEntity<List<Company>> getAllCompanies() {
        List<Company> companies = companyService.findAll();
        return new ResponseEntity<>(companies, HttpStatus.OK);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateCompany(@PathVariable Integer id, @RequestBody Company company) {
        company.setId(id);
        int result = companyService.update(company);
        if (result > 0) {
            return new ResponseEntity<>(result, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 根据id获取公司信息
    @GetMapping("/{id}")
    public ResponseEntity<Company> getCompanyById(@PathVariable Integer id) {
        Company company = companyService.findById(id);
        if (company != null) {
            return new ResponseEntity<>(company, HttpStatus.OK);
        } else {
            return new ResponseEntity<>(HttpStatus.NOT_FOUND);
        }
    }

    // 保存公司信息
    @PostMapping
    public ResponseEntity<Integer> saveCompany(@RequestBody Company company) {
        int result = companyService.save(company);
        return new ResponseEntity<>(result, HttpStatus.CREATED);
    }

    // 更新公司信息
    @PutMapping
    public ResponseEntity<Integer> updateCompany(@RequestBody Company company) {
        int result = companyService.update(company);
        return new ResponseEntity<>(result, HttpStatus.OK);
    }


}